package cn.wolfcode.shop.cloud.web.advice;

import cn.wolfcode.shop.cloud.common.CommonControllerAdvice;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.util.MemberCodeMsg;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MemberContorllerAdvice extends CommonControllerAdvice {

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handler(BindException e){
        //参数异常信息:{0}
        String message = e.getAllErrors().get(0).getDefaultMessage();//手机格式不正确
        System.out.println(message);
        MemberCodeMsg memberCodeMsg = MemberCodeMsg.PARAM_OPTION;
        //参数异常信息:手机格式不正确

        return Result.error(memberCodeMsg.format(message));
    }
}
